package org.example.covid.group;

import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
import org.example.covid.entity.CountyEntity;

/**
 * todo:MapReduce中自定义分组的类
 */
public class CountyGroupingComparator extends WritableComparator {

    protected CountyGroupingComparator(){
        //是否允许反射创建对象实例
        super(CountyEntity.class,true);
    }

    @Override
    public int compare(WritableComparable a, WritableComparable b) {
        //类型转换
        final CountyEntity aBean = (CountyEntity) a;
        final CountyEntity bBean = (CountyEntity) b;
        //分组规则，只要两个数据的state一样，应该分到同一组
        //compare返回0， mapreduce框架就认为两个一样，分为一组
        return aBean.getState().compareTo(bBean.getState());

    }
}
